package com.zw.pdm.module.est.controller;

import com.zw.pdm.annotation.RepeatCommit;
import com.zw.pdm.base.BaseController;
import com.zw.pdm.common.JsonResult;
import com.zw.pdm.entity.craft.CraMakeSheet;
import com.zw.pdm.entity.est.Card;
import com.zw.pdm.module.basic.service.DictCommonService;
import com.zw.pdm.module.est.model.CardModel;
import com.zw.pdm.module.est.service.WorkTimeService;
import com.zw.pdm.module.report.service.SheetService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.zw.pdm.constant.Constant.GROUP_SPECTYPE;

/**
 * @Description: 工艺卡工时编制
 *
 * @Author: China.sgl
 * @Date: 2020/8/3 16:11
 */
@Controller
@RequestMapping("/est/worktime")
@SuppressWarnings("all")
public class WorkTimeController extends BaseController {

    @Autowired
    private WorkTimeService workTimeService;
    @Autowired
    private DictCommonService dictCommonService;
    @Autowired
    private SheetService sheetService;


    @GetMapping
    @RequiresPermissions(value = {"est:worktime:view"}, logical = Logical.OR)
    public String page(Model model) {
        model.addAttribute("specials", dictCommonService.findDictByGroupName(GROUP_SPECTYPE));
        return "est/worktime/page";
    }

    /** 列表 */
    @ResponseBody
    @PostMapping("/list")
    public JsonResult cardList(CraMakeSheet craMakeSheet) {
        startPage();
        List<CraMakeSheet> list = workTimeService.findWithWorkTime(craMakeSheet);
        return jsonResult(list);
    }

    /** 子列表 */
    @ResponseBody
    @PostMapping("/slaveList")
    public JsonResult slaveList(CardModel card) {
        startPage();
        List<CardModel> list = workTimeService.findSlaveWithWorkTime(card);
        return jsonResult(list);
    }

    /** 工时编制页面  */
    @GetMapping("/{id}")
    @RequiresPermissions({"est:worktime:edit"})
    public String info(@PathVariable("id") Integer id, Model model) {
        model.addAttribute("card", workTimeService.getCardById(id));
        model.addAttribute("details", workTimeService.findCardDetailByCardId(id));
        return "est/worktime/info";
    }

    /** 保存工艺卡 */
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions(value = {"est:worktime:edit"}, logical = Logical.OR)
    public JsonResult save(Card card, Integer[] detailId, Double[] workTimeOne, Double[] workTimeTwo) {
        return jsonResult(workTimeService.save(card, detailId, workTimeOne, workTimeTwo));
    }

    /** 准备提交审核 */
    @ResponseBody
    @RepeatCommit
    @PostMapping("/preCommit")
    @RequiresPermissions({"est:worktime:commit"})
    public JsonResult preCommit(Integer id) {
        return jsonResult(workTimeService.preCommit(id));
    }

    /** 提交审核 */
    @ResponseBody
    @RepeatCommit
    @PostMapping("/commit")
    @RequiresPermissions({"est:worktime:commit"})
    public JsonResult commit(Integer id) {
        return jsonResult(workTimeService.commit(id));
    }

    /** 撤消提交 */
    /*@ResponseBody
    @RepeatCommit
    @PostMapping("/cancelCommit")
    @RequiresPermissions({"est:worktime:cancel"})
    public JsonResult cancelCommit(Integer id) {
        return jsonResult(workTimeService.cancelCommit(id));
    }*/

    /** 工时不需要  */
    @ResponseBody
    @RepeatCommit
    @PostMapping("/need")
    @RequiresPermissions({"est:worktime:need"})
    public JsonResult need(Integer id) {
        return jsonResult(workTimeService.need(id));
    }

    /** 驳回  */
    @ResponseBody
    @RepeatCommit
    @PostMapping("/reject")
    @RequiresPermissions({"est:worktime:cancel"})
    public JsonResult reject(Integer id) {
        return jsonResult(workTimeService.reject(id));
    }

}
